package com.sansan.qiangji.realize.device;

import com.sansan.qiangji.entity.device.StackerParamDetail;
import com.sansan.qiangji.enums.ResultEnum;
import com.sansan.qiangji.exception.ServiceException;
import com.sansan.qiangji.service.impl.device.StackerParamDetailServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class StackerParamDetailReal {
    private StackerParamDetailServiceImpl stackerParamDetailService;

    @Resource
    public void setStackerParamDetailService(StackerParamDetailServiceImpl stackerParamDetailService) {
        this.stackerParamDetailService = stackerParamDetailService;
    }

    @Transactional
    public void changeStackerParamDetailValue(String stackerName, String stackerParamName, Long stackerParamChangeValue) {
        List<StackerParamDetail> stackerParamDetails = stackerParamDetailService.findByStackerName(stackerName);
        stackerParamDetails = stackerParamDetails.stream().filter(e -> e.getStackerParamName().equals(stackerParamName)).collect(Collectors.toList());
        if (stackerParamDetails.size() == 0) {
            log.error("【修改堆垛机参数值】堆垛机参数不存在");
            throw new ServiceException(ResultEnum.STACKER_PARAM_DETAIL_IS_NOT_EXIST);
        }
        if (stackerParamDetails.get(0).getStackerParamType().equals("bool")) {
            if (stackerParamChangeValue != 0 && stackerParamChangeValue != 1) {
                log.error("【修改堆垛机参数值】堆垛机参数值只能是 0 或 1 !");
                throw new ServiceException(ResultEnum.STACKER_PARAM_DETAIL_VALUE_ONLY_CAN_ZERO_OR_ONE);
            }
        }
        stackerParamDetails.get(0).setStackerParamChangeValue(stackerParamChangeValue);
        stackerParamDetailService.updateStackerParamDetail(stackerParamDetails.get(0));
    }
}
